<?php

/**
 * BKTiny Framework
 * Core_Database abstract class
 * 
 * LICENSE:
 * BKTiny Framework duoc xay dung va phat trien boi nhom BKloud - dai hoc bach khoa Ha Noi 
 * Ban duoc phep su dung framework nay cho muc dich hoc tap , nghien cuu va cac ung dung phi thuong mai
 * 
 * @copyright Copyright (c) 2011 BKloud - HPCC.
 * @version 0.1
 */
abstract class Core_Database
{
    /**
     * PDO to use
     *
     * @var PDO
     */
    protected $_db;

    public function __construct()
    {
        $registry = Core_Registry::singleton();
        $db       = $registry['config']['database'];

        $host     = $db['host'];
        $dbname   = $db['dbname'];
        $username = $db['username'];
        $password = $db['password'];

        $dsn = "mysql:dbname=$dbname;host=$host";

        try
        {
            $this->_db = new PDO($dsn, $username, $password, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''));
        }
        catch (PDOException $e)
        {
            $this->_db = null;
        }

        if ($this->_db == null)
            throw new Exception("Failed to connect to database");
    }
}
